def pr(x):
    f = 0
    for i in range(2,x):
        if x % i == 0:
            f= f + 1
    if f != 0:
        return False
    return True
def vh(x,b):
    if str(x) in str(b):
        return True
    return False

b = int(input())
n = 0
if 999 < b <= 9999:
    for i in range(b+1):
        if pr(i) and vh(i,b):
            if n < i:
                n = i
print(n)
